const users=require("../database/users")
const  jwt=require("jsonwebtoken")
const md5=require("md5")
const tk=require("./token")

module.exports=async (req,res)=>{
  const {tel,password}=req.query
  const userData=await users.find({tel})
  try{
  if(userData.length<1){
    res.send({
      code:3,
      msg:"该手机号未注册",
      data:null
    })
  }else{
    if(userData[0].password===md5(password)){
      res.send({
        code:1,
        msg:"登陆成功",
        data:tk(userData[0])
      })
    }else{
      res.send({
        code:2,
        msg:"密码错误",
        data:null
      })
    }
  }
  }catch(err){
    res.send({
      code:0,
      msg:"数据库出错",
      data:err.toString()
    })
  }
}
